using System;
using Microsoft.SPOT;
using Komodex.NETMF.MicroTweet.HTTP;

namespace DCDev.Attendant.Actions
{
    class ThingSpeakAction : IAttendantAction
    {
        private const string _apiKey = "";
        private const int _delayInSeconds = 15;
        
        DateTime _lastNotification = DateTime.MinValue;        

        #region IAttendantAction Members

        public bool Notify(bool isOpen)
        {
            if (_lastNotification.AddSeconds(_delayInSeconds) < DateTime.Now)
            {
                Debug.Print("Sending ThingSpeak Update");

                string url = "http://api.thingspeak.com/update?key=" + _apiKey + "&field1=" + (isOpen ? "1" : "0");

                HttpRequest request = new HttpRequest(url);
                request.Method = "GET";
                HttpResponse response = request.GetResponse();

                if (response.ResponseCode == 200)
                {
                    _lastNotification = DateTime.Now;
                    return true;
                }
            }
            
            return false;            
        }

        #endregion
    }
}
